package com.meizi.mall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meizi.mall.product.dao.CategoryDao;
import com.meizi.mall.product.entity.CategoryEntity;
import com.meizi.mall.product.service.CategoryService;
import com.meizi.core.tool.api.PageUtils;
import com.meizi.core.tool.api.Query;
import com.meizi.mall.product.vo.Catalog3Vo;
import com.meizi.mall.product.vo.CatalogL2Vo;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWhitTree() {
        List<CategoryEntity> entities = baseMapper.selectList(null);
        List<CategoryEntity> level1Menus = entities.stream().filter((entity) -> {
            return entity.getParentCid() == 0;
        }).map((entity) -> {
            entity.setChildren(getChildrens(entity, entities));
            return entity;
        }).sorted((e1, e2) -> {
            return (e1.getSort() == null ? 0 : e1.getSort()) - (e2.getSort() == null ? 0 : e2.getSort());
        }).collect(Collectors.toList());
        return level1Menus;
    }

    @Override
    public void removeMenuByIds(List<Long> asList) {
        baseMapper.deleteBatchIds(asList);
    }

    @Override
    public List<Long> findCatelogPath(Long catelogId) {
        List<Long> paths = new ArrayList<>();
        paths = findParentPath(catelogId,paths);
        Collections.reverse(paths);
        return paths;
    }

    @Cacheable(value = {"category"},key = "#root.methodName")
    @Override
    public List<CategoryEntity> getLevel1Cate() {

        List<CategoryEntity> l1Categorys = baseMapper.selectList(new QueryWrapper<CategoryEntity>().eq("parent_cid", 0));
        return l1Categorys;
    }
    @Cacheable(value = {"category"},key="#root.methodName")
    @Override
    public Map<String, List<CatalogL2Vo>> catalogJson() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        Map<String, List<CatalogL2Vo>> collect = categoryEntities.stream()
                .filter(l1 -> l1.getParentCid() == 0)
                .collect(Collectors.toMap(l1 -> l1.getCatId().toString(),
                        l1 -> getSubList(l1.getCatId(), categoryEntities)
                                .stream()
                                .map(l2 -> {
                                            CatalogL2Vo catalogL2Vo = new CatalogL2Vo();
                                            List<Catalog3Vo> l3s = getSubList(l2.getCatId(), categoryEntities).stream().map(l3 -> {
                                                Catalog3Vo catalog3Vo = new Catalog3Vo();
                                                catalog3Vo.setCatalog2Id(l2.getCatId().toString());
                                                catalog3Vo.setId(l3.getCatId().toString());
                                                catalog3Vo.setName(l3.getName());
                                                return catalog3Vo;
                                            }).collect(Collectors.toList());
                                            catalogL2Vo.setCatalog1Id(l1.getCatId().toString());
                                            catalogL2Vo.setCatalog3List(l3s);
                                            catalogL2Vo.setId(l2.getCatId().toString());
                                            catalogL2Vo.setName(l2.getName());
                                            return catalogL2Vo;
                                        }
                                ).collect(Collectors.toList())

                ));
        return collect;
    }
    //    @CacheEvict(value = "category",key="getLevel1Cate")
//    @Caching(evict = {
//            @CacheEvict(value = "category",key = "'getLevel1Cate'"),
//            @CacheEvict(value = "category",key = "'catalogJson'")
//    })
    @CacheEvict(value = "category",allEntries = true)
    @Override
    public void updateCategory(CategoryEntity category) {
        updateById(category);
    }

    private List<CategoryEntity> getSubList(Long parent_id,List<CategoryEntity> all){
        return     all.stream().filter(entity->entity.getParentCid().equals(parent_id)).collect(Collectors.toList());
    }

    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        paths.add(catelogId);
        CategoryEntity categoryEntity = baseMapper.selectById(catelogId);
        if (categoryEntity.getParentCid() != 0) {
            findParentPath(categoryEntity.getParentCid(),paths);
        }
        return paths;
    }

    private List<CategoryEntity> getChildrens(CategoryEntity entity, List<CategoryEntity> all) {
        List<CategoryEntity> children = all.stream().filter((e) -> {
            return entity.getCatId().equals( e.getParentCid());
        }).map((e) -> {
            e.setChildren(getChildrens(e, all));
            return e;
        }).sorted((e1, e2) -> {
            return (e1.getSort() == null ? 0 : e1.getSort()) - (e2.getSort() == null ? 0 : e2.getSort());
        }).collect(Collectors.toList());
        return children;
    }

}